package com.house.system.service.impl;

import java.util.List;

import com.house.common.annotation.DataScope;
import com.house.common.core.domain.entity.House;
import com.house.common.utils.DateUtils;
import com.house.system.domain.Contract;
import com.house.system.domain.RentalType;
import com.house.system.mapper.ContractMapper;
import com.house.system.mapper.HouseMapper;
import com.house.system.mapper.RentalTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.house.system.mapper.QuitMapper;
import com.house.system.domain.Quit;
import com.house.system.service.IQuitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退租对象Service业务层处理
 *
 * @author tc
 * @date 2022-02-25
 */
@Service
public class QuitServiceImpl implements IQuitService {
    @Autowired
    private QuitMapper quitMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private RentalTypeMapper rentalTypeMapper;

    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询退租对象
     *
     * @param quitId 退租对象主键
     * @return 退租对象
     */
    @Override
    public Quit selectQuitByQuitId(Long quitId) {
        return quitMapper.selectQuitByQuitId(quitId);
    }

    /**
     * 查询退租对象列表
     *
     * @param quit 退租对象
     * @return 退租对象
     */
    @Override
    @DataScope(tableAlias = "q")
    public List<Quit> selectQuitList(Quit quit) {
        return quitMapper.selectQuitList(quit);
    }

    /**
     * 新增退租对象
     *
     * @param quit 退租对象
     * @return 结果
     */
    @Override
    public int insertQuit(Quit quit) {
        quit.setCreateTime(DateUtils.getNowDate());
        return quitMapper.insertQuit(quit);
    }

    /**
     * 修改退租对象
     *
     * @param quit 退租对象
     * @return 结果
     */
    @Override
    public int updateQuit(Quit quit) {
        quit.setUpdateTime(DateUtils.getNowDate());
        return quitMapper.updateQuit(quit);
    }

    /**
     * 批量删除退租对象
     *
     * @param quitIds 需要删除的退租对象主键
     * @return 结果
     */
    @Override
    public int deleteQuitByQuitIds(Long[] quitIds) {
        return quitMapper.deleteQuitByQuitIds(quitIds);
    }

    /**
     * 删除退租对象信息
     *
     * @param quitId 退租对象主键
     * @return 结果
     */
    @Override
    public int deleteQuitByQuitId(Long quitId) {
        return quitMapper.deleteQuitByQuitId(quitId);
    }

    /**
     * 中介处理退租
     *
     * @param quitId  退租对象编号
     * @param houseId 房屋对象编号
     * @return 结果
     */
    @Override
    @Transactional
    public int handleQuit(Long quitId, Long houseId) {
        //修改租售类型对象
        RentalType rentalType = rentalTypeMapper.selectRentalTypeByHouseId(houseId);
        //获取已租人数
        int rented = rentalType.getRented();
        //已租人数-1表示退租
        rented = rented > 0 ? rented - 1 : rented;
        rentalType.setRented(rented);
        rentalTypeMapper.updateRentalType(rentalType);

        //修改租赁合同
        Contract contract = contractMapper.selectContractByHouseId(houseId);
        contract.setStatus("1");
        contractMapper.updateContract(contract);

        House house = houseMapper.selectHouseByHouseId(houseId);
        house.setIsRental("1");
        houseMapper.updateHouse(house);

        Quit quit = quitMapper.selectQuitByQuitId(quitId);
        quit.setStatus("1");
        return quitMapper.updateQuit(quit);
    }
}
